package com.hyy.dao.daoImpl;

import com.hyy.bean.MaskUser;
import com.hyy.bean.Merchandise;
import com.hyy.dao.MerchandiseDao;
import com.hyy.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MerchandiseDaoImpl implements MerchandiseDao {


	//查询所有商品
	@Override
	public List<Merchandise> findAllProducts() throws SQLException {
		List<Merchandise> merchandises = new ArrayList<Merchandise>();
		Connection conn = JdbcUtil.getConnection();
		String sql = "select id,productName,price,stock,img,categoryid,sales from merchandise";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		ResultSet rs = pstmt.executeQuery();
		while (rs.next()){
			merchandises.add(new Merchandise(rs.getInt(1),rs.getString(2),rs.getDouble(3),
					rs.getInt(4),rs.getString(5),rs.getLong(6),rs.getLong(7)));
		}
		JdbcUtil.release(conn,pstmt,rs);

		return merchandises;
	}

	//商品名称关键字模糊查询
	@Override
	public List<Merchandise> findProductByKeyword(String keyword) throws SQLException {
		List<Merchandise> merchandises = new ArrayList<Merchandise>();
		Connection conn = JdbcUtil.getConnection();
		String sql = "select id,productName,price,stock,img,categoryid,sales from merchandise where " +
				"productName like  \"%\"?\"%\"";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setString(1,keyword);
		ResultSet rs = pstmt.executeQuery();
		while (rs.next()){
			merchandises.add(new Merchandise(rs.getInt(1),rs.getString(2),rs.getDouble(3),
					rs.getInt(4),rs.getString(5),rs.getLong(6),rs.getLong(7)));
		}
		JdbcUtil.release(conn,pstmt,rs);

		return merchandises;
	}

	@Override
	public Merchandise findProductById(Long id) throws SQLException {
		Connection conn = JdbcUtil.getConnection();
		String sql = "select id,productName,price,stock,img,categoryid,sales from merchandise where " +
				"id " +
				"= ?";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setLong(1,id);
		ResultSet rs = pstmt.executeQuery();
		while (rs.next()){
			Merchandise merchandise =  new Merchandise(rs.getInt(1),rs.getString(2),rs.getDouble(3),
					rs.getInt(4),rs.getString(5),rs.getLong(6),rs.getLong(7));
			JdbcUtil.release(conn,pstmt,rs);
			return merchandise;
		}
		return null;
	}

	@Override
	public boolean outProductById(Long  id, Long  num) throws SQLException {
		Connection conn = JdbcUtil.getConnection();
		String sql = "update merchandise set stock = stock-? where id = ?";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setLong (1,num);
		pstmt.setLong (2,id);
		return pstmt.executeUpdate() != 0;
	}

	@Override
	public boolean inProductById(Long  id, Long  num) throws SQLException {
		return false;
	}

	@Override
	public boolean addProduct(Merchandise merchandise) throws SQLException {
		Connection conn = JdbcUtil.getConnection();
		String sql = "insert into merchandise(productName, price, stock,sales, img, categoryid) " +
				"values (?,?,?,?,?,?) ";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setString(1,merchandise.getProductName());
		pstmt.setDouble(2,merchandise.getPrice());
		pstmt.setLong(3,merchandise.getStock());
		pstmt.setLong(4,merchandise.getSales());
		pstmt.setString(5,merchandise.getImg());
		pstmt.setLong(6,merchandise.getCategoryid());

		int n = pstmt.executeUpdate();
		JdbcUtil.release(conn,pstmt);
		return n==1?true:false;

	}

	@Override
	public boolean deleteProduct(Long  id) throws SQLException {
		Connection conn = JdbcUtil.getConnection();
		String sql="delete from merchandise where id = ?";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setLong(1,id);
		int n =pstmt.executeUpdate();
		JdbcUtil.release(conn,pstmt);

		return n==1?true:false;
	}

	@Override
	public boolean updateProduct(Merchandise merchandise) throws SQLException {

		Connection conn = JdbcUtil.getConnection();
		String sql = "update merchandise set productName = ? ,price=?,stock=?,img=?,sales=?," +
				"categoryid=?  " +
				"where " +
				"id=?";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setString(1,merchandise.getProductName());
		pstmt.setDouble(2,merchandise.getPrice());
		pstmt.setLong(3,merchandise.getStock());
		pstmt.setString(4,merchandise.getImg());
		pstmt.setLong(5,merchandise.getSales());
		pstmt.setLong(6,merchandise.getCategoryid());
		pstmt.setLong(7,merchandise.getId());

		int n = pstmt.executeUpdate();
		JdbcUtil.release(conn,pstmt);
		return n==1?true:false;

	}
}
